package learn.sort;

import java.util.Arrays;

/**
 * @author 于昂
 * Date 2021/1/12
 * Time 19:34
 */
public class ShellSort {
    public static void main(String[] args) {
//        int[] array = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
        int[] array = new int[80000];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int) (Math.random() * 800000);
        }
        Long startTime = System.currentTimeMillis();
        moveSort(array);
        Long endTime = System.currentTimeMillis();
        long tempTime = (endTime - startTime);
        System.out.println("花费时间：" +
                (((tempTime / 86400000) > 0) ? ((tempTime / 86400000) + "d") : "") +
                ((((tempTime / 86400000) > 0) || ((tempTime % 86400000 / 3600000) > 0)) ? ((tempTime % 86400000 / 3600000) + "h") : ("")) +
                ((((tempTime / 3600000) > 0) || ((tempTime % 3600000 / 60000) > 0)) ? ((tempTime % 3600000 / 60000) + "m") : ("")) +
                ((((tempTime / 60000) > 0) || ((tempTime % 60000 / 1000) > 0)) ? ((tempTime % 60000 / 1000) + "s") : ("")) +
                ((tempTime % 1000) + "ms"));
//        System.out.println(Arrays.toString(array));
    }

    public static void moveSort(int[] array) {
        for (int gap = array.length / 2; gap > 0; gap /= 2) {
            //从gap个元素开始逐个对其所在的组进行直接插入排序
            for (int i = gap; i < array.length; i++) {
                int index = i;//记录索引位置
                int temp = array[index];//记录临时值
                if (array[index] < array[index - gap]) {
                    while (index - gap >= 0 && (temp < array[index - gap])) {
                        array[index] = array[index - gap];
                        index -= gap;
                    }
                    array[index] = temp;
                }
            }
        }
    }

    /**
     * @param array 数组
     *              交换排序法
     */
    public static void exchangeSort(int[] array) {
        //gap 步长 初始步长为array.length / 2 而后每次/2
        for (int gap = array.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < array.length; i++) {
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (array[j] > array[j + gap]) {
                        int temp = array[j];
                        array[j] = array[j + gap];
                        array[j + gap] = temp;
                    }
                }
            }
        }
//        System.out.println(Arrays.toString(array));
    }

    /**
     * 逐步推导
     * 组数与步长是一个值
     *
     * @param array 数组*
     */
    public static void base(int[] array) {
        //array.length=10 10/2=5 分五组
        //从index5 开始遍历
        for (int i = 5; i < array.length; i++) {
            //遍历的步长为5
            for (int j = i - 5; j >= 0; j -= 5) {
                //如果当前元素大于下个步长的元素 则交换
                if (array[j] > array[j + 5]) {
                    int temp = array[j];
                    array[j] = array[j + 5];
                    array[j + 5] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(array));
        //5/2 = 2 分为两组
        for (int i = 2; i < array.length; i++) {
            //步长为2
            for (int j = i - 2; j >= 0; j -= 2) {
                if (array[j] > array[j + 2]) {
                    int temp = array[j];
                    array[j] = array[j + 2];
                    array[j + 2] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(array));

        //2/2=1 不再分组
        for (int i = 1; i < array.length; i++) {
            //步长为1
            for (int j = i - 1; j >= 0; j -= 1) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(array));
    }

    public static void base2(int[] array) {
        //分5组遍历
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 2; j++) {
                if (array[i] > array[i + 5]) {
                    int temp = array[i];
                    array[i] = array[i + 5];
                    array[i + 5] = temp;
                }
            }
        }
    }
}
